﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using System.Xml;
using System.Data.Common;
using System.IO;
using System.Drawing;
using System.Diagnostics;

namespace MacomberMapSystem.Common.Integration
{
    /// <summary>
    /// This class holds information on a district boundary
    /// </summary>
    [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.None)]
    public class MM_District : MM_Boundary
    {
        /// <summary>
        /// Intiialize a new district from XML
        /// </summary>
        /// <param name="BaseElement"></param>
        /// <param name="Repository"></param>
        public MM_District(XmlElement BaseElement, MM_Repository Repository)
            : base(BaseElement, Repository)
        { }

        /// <summary>
        /// Initialize a new district from RDB
        /// </summary>
        /// <param name="DataReader"></param>
        /// <param name="Repository"></param>
        public MM_District(DbDataReader DataReader, MM_Repository Repository)
            : base(DataReader, Repository)
        { }

        /// <summary>
        /// Initialize an empty district
        /// </summary>
        public MM_District()
        { }

        /// <summary>
        /// Extract a district, and add to our database.
        /// </summary>
        /// <param name="DirectoryToRead">The directory in which the files are held</param>
        /// <returns></returns>
        public static MM_District[] ExtractDistricts(DirectoryInfo DirectoryToRead)
        {
            //Use our shapefile converter to read in our components
            List<MM_District> OutDistricts = new List<MM_District>();
            List<PointF> BoundaryPoints = new List<PointF>();

            foreach (FileInfo ShapeToRead in DirectoryToRead.GetFiles("*.shp"))
            {
                BoundaryPoints.Clear();

                ProcessStartInfo psI = new ProcessStartInfo(Path.Combine(ShapeToRead.DirectoryName, "shp2text.exe"), "--spreadsheet \"" + ShapeToRead.FullName + "\"");
                psI.RedirectStandardOutput = true;
                psI.UseShellExecute = false;
                psI.WorkingDirectory = ShapeToRead.DirectoryName;
                Process ps = Process.Start(psI);
                String DistrictName = "";
                using (StreamReader sRd = ps.StandardOutput)
                    while (!sRd.EndOfStream)
                    {
                        String[] inLine = sRd.ReadLine().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        if ((inLine.Length > 0) && (inLine[0] != "id"))
                        {
                            BoundaryPoints.Add(new PointF(float.Parse(inLine[1]), float.Parse(inLine[2])));
                            DistrictName = inLine[8];
                        }
                    }
                if (BoundaryPoints.Count > 0)
                {
                    MM_District NewDistrict = new MM_District();
                    NewDistrict.Name = DistrictName;
                    NewDistrict.ID = OutDistricts.Count;
                    NewDistrict.Coordinates = BoundaryPoints.ToArray();
                    NewDistrict.RecomputePoints();
                    NewDistrict.RecomputeExtrema();                   
                    OutDistricts.Add(NewDistrict);
                }
                System.Windows.Forms.Application.DoEvents();
            }
            return OutDistricts.ToArray();
        }
    }
}